package com.vkmessenger.gui.activities.auth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.vkmessenger.R;
import com.vkmessenger.core.Executor;
import com.vkmessenger.gui.activities.ContainerActivity;
import com.vkmessenger.gui.activities.contacts.FriendsActivity;
import com.vkmessenger.net.clients.LongPollClient;
import com.vkmessenger.net.clients.WebClient;

import java.util.logging.Logger;

public class LoginActivity extends Activity {
    private static Logger logger = Logger.getLogger(LoginActivity.class.getName());

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        linkComponents();
    }

    public void linkComponents() {
        final View imagePass = findViewById(R.id.image_pass);
        findViewById(R.id.edit_pass).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean b) {
                imagePass.setSelected(b);
            }
        });

        final View imagePhone = findViewById(R.id.image_phone);
        findViewById(R.id.edit_phone_number).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean b) {
                imagePhone.setSelected(b);
            }
        });

        findViewById(R.id.button_enter_registration).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


        final EditText phoneNumber = (EditText) findViewById(R.id.edit_phone_number);
        final EditText passwordEdit = (EditText) findViewById(R.id.edit_pass);
        findViewById(R.id.button_enter).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                invokeLogin(phoneNumber.getText().toString(), passwordEdit.getText().toString());
            }
        });
    }


    public void invokeLogin(String login, String password) {
        //todo  it's the stub to test login. These actions are to be moved to web interation class
        WebClient.INSTANCE.logIn(login, password, new Runnable() {
            public void run() {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, ContainerActivity.class);
                startActivity(intent);

                LongPollClient longPollClient = new LongPollClient();
                longPollClient.start();

                Executor.service.submit(new Runnable() {
                    public void run() {
                        WebClient.INSTANCE.loadFriends(5, FriendsActivity.FIELDS_NEEDED_FOR_LIST, true);
                        WebClient.INSTANCE.loadFriends(null, FriendsActivity.FIELDS_NEEDED_FOR_LIST, false);
                    }
                });
            }
        });
    }
}
